package com.starblink.wishlist.wishallitems.ui.adapter

import androidx.core.view.updateLayoutParams
import androidx.recyclerview.widget.RecyclerView
import com.google.android.flexbox.FlexboxLayoutManager
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.visible
import com.starblink.basic.style.view.round.CornerType
import com.starblink.basic.util.CommUtils
import com.starblink.wishlist.databinding.CellFilterItemBinding
import com.starblink.wishlist.wishallitems.ui.bean.RvFilterChildEntry

/**
 */
class FilterLayAdapter(
    parent: RecyclerView,
    val data: List<RvFilterChildEntry>,
    private val onItemClick: (model: RvFilterChildEntry, position: Int) -> Unit
) :
    MutableAdapter(data) {



    init {
        addVhDelegate { it ->
            FilterLayCell(
                CellFilterItemBinding.inflate(
                    it.inflater,
                    it,
                    false
                )
            ) { model, position ->
                onItemClick.invoke(model, position)
            }
        }
    }

}


class FilterLayCell(
    binding: CellFilterItemBinding,
    private val onClick: (model: RvFilterChildEntry, position: Int) -> Unit
) : BaseBindingVH<RvFilterChildEntry, CellFilterItemBinding>(binding) {

    init {
        itemView.updateLayoutParams<FlexboxLayoutManager.LayoutParams> {
//            alignSelf = AlignSelf.FLEX_START
        }
    }

    private val choiceBorderColor = CommUtils.getColor(com.starblink.basic.style.R.color.sk_red600)
    private val noChoiceBorderColor = CommUtils.getColor(com.starblink.basic.style.R.color.sk_gray0)
    private val noChoiceTextColor = CommUtils.getColor(com.starblink.basic.style.R.color.sk_gray500)

    override fun onBind(model: RvFilterChildEntry, position: Int) {
        binding.btnTxt.text = model.name
        binding.btn.setOnClickListener {
            onClick.invoke(model, position)
        }
        if (model.choice) {
            binding.btn.setCornerRadius(
                choiceBorderColor.toFloat(),
                CornerType.R_BORDER_COLOR
            )
            binding.btn.setBackgroundResource(com.starblink.basic.style.R.color.sk_red50)
            binding.btnTxt.setTextColor(choiceBorderColor)
            binding.btnTag.visible()
        } else {
            binding.btn.setCornerRadius(
                noChoiceBorderColor.toFloat(),
                CornerType.R_BORDER_COLOR
            )
            binding.btn.setBackgroundResource(com.starblink.basic.style.R.color.sk_gray0)
            binding.btnTxt.setTextColor(noChoiceTextColor)
            binding.btnTag.gone()
        }
    }

}
